package com.algorithm.twohundred2threehundred;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: tzj
 * @create: 2021-06-11
 **/
public class MyStack_225 {
    private static Queue<Integer> queue = new LinkedList<>();

    public static void main(String[] args) {
        queue.add(1);
        queue.add(2);
        System.out.println(queue.peek());
    }

    /**
     * Push element x onto stack.
     */
    public void push(int x) {
        int n = queue.size();
        // 添加一个元素，队列满了就返回false
        queue.offer(x);
        for (int i = 0; i < n; i++) {
            queue.offer(queue.poll());
        }
    }

    /**
     * Removes the element on top of the stack and returns that element.
     */
    public int pop() {
        // 移除一个元素，没有的话返回null,remove方法没有的话抛异常
        return queue.poll();
    }

    /**
     * Get the top element.
     */
    public int top() {
        // 查询头部元素，没有的话返回null
        return queue.peek();
    }

    /**
     * Returns whether the stack is empty.
     */
    public boolean empty() {
        return queue.isEmpty();
    }

}
